﻿using System;
using System.Web;
using SAS.Bussiness;
using SAS.Core.Entity;
using SAS.Core.Result;
using SAS.WebSite.BaseClasses;

namespace SAS.WebSite.VisualControls.Other
{
    public partial class Login : System.Web.UI.UserControl
    {   
        protected void Page_Load(object sender, EventArgs e)
        {
            //user = ((BasePage)Page).User();
            //Status = Request.QueryString["status"];
            //LoggedOut = Request.QueryString["logout"];
            //TimedOut = Request.QueryString["timeout"];
            //bool rememberusername = true;
            //if ((Status != null)&&(Status.Length > 0))
            //{
            //    switch (Status.ToLower())
            //    {
            //        case "timedout":
            //            litLOMessage.Text = "<h1>Session Time-Out</h1>You have been automatically logged out due to prolonged inactivity.";
            //            break;
            //        case "invalidtoken":
            //            litLOMessage.Text = "<h1>Forced LogOff</h1>Your authorization token does not match.  This could be a result of being logged in to your account using two different computers or browsers.";
            //            break;
            //        case "logout":
            //            litLOMessage.Text = "<h1>You have been successfully logged out</h1>";
            //            break;
            //    }
            //}
            //else if (LoggedOut == "true")
            //{
            //    litLOMessage.Text = "<h1>You have been successfully logged out</h1>";
            //}
            //else if (TimedOut == "true")
            //{
            //    litLOMessage.Text = "<h1>Session Time-Out</h1>You have been automatically logged out due to prolonged inactivity.";
            //}
            //else
            //{
            //    if (user.Authenticated)
            //    {
            //        EDD.BLL.ClientConfig cli = new EDD.BLL.ClientConfig(user.pkClientConfig_ID);
            //        HttpContext.Current.Session["Client"] = cli;
            //        Response.Redirect(cli.MainURL);
            //    }
            //}
            //clearSession();
            //if (!IsPostBack)
            //{
            //    if (((BasePage)Page).KeepUserName.Length > 0)
            //    {
            //        if (((BasePage)Page).KeepUserName == "forget")
            //        {
            //            rememberusername = false;
            //        }
            //    }
            //    chkRememberMe.Checked = rememberusername;
            //    if (rememberusername)
            //    {
            //        if (((BasePage)Page).UserName.Length > 0)
            //        {
            //            txtUserName.Text = ((BasePage)Page).UserName;
            //        }
            //    }
            //    else
            //    {
            //        ((BasePage)Page).ClearUserCookie();
            //    }
            //}
            if (hidSecret.Value.Length <= 0)
            {
                hidSecret.Value = Guid.NewGuid().ToString();
            }

        }

        protected void clearSession()
        {
            ((BasePage)Page).ClearSession();
        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            //BasePage basePage = (BasePage)Page;
            //Response.Redirect("Pages//Home.aspx");

            string email = txtUserName.Text.ToLower();

            User userLoginInfo = new User();
            userLoginInfo.Email = email;
            userLoginInfo.HashPass = hidHash.Value;
            userLoginInfo.Secret = hidSecret.Value;
            UserBO userBo = new UserBO();

            string strHash = hidHash.Value;
            string strSecret = hidSecret.Value;
            //SASResult<User> userResult = userBo.Login(userLoginInfo, strHash, strSecret);

            //if (userResult.IsSucessful && userResult.Result != null && userResult.Result.IsActive)
            //if (userResult.IsSucessful && userResult.Result != null)
            //if (1==1)
            //{
                //User.Hash = hidHash.Value;
                //user.Secret = hidSecret.Value;
                HttpContext.Current.Session["User"] = new User();
                Response.Redirect("~/Pages/Workspace.aspx");
            //}
            //else
            //{
            //    litLOMessage.Text = "<h1>Login Failed.</h1>Please check your login name and try logging in again.";
            //}

            ////this will need to be removed and a javascript equivalant will need to be used
            //if (chkRememberMe.Checked)
            //{
            //    ((BasePage)Page).KeepUserName = "remember";
            //    ((BasePage)Page).UserName = txtUserName.Text;
            //}
            //else
            //{
            //    ((BasePage)Page).KeepUserName = "forget";
            //    ((BasePage)Page).ClearUserCookie();
            //}

            //user.Email = txtUserName.Text.ToLower();
            //user.Hash = hidHash.Value;
            //user.Secret = hidSecret.Value;
            //user.Login();
            //if (user.Authenticated)
            //{
            //    ((BasePage)Page).SetUser(user);
            //    ClientConfig cli = new ClientConfig(user.pkClientConfig_ID);
            //    HttpContext.Current.Session["Client"] = cli;
            //    Response.Redirect(cli.MainURL);
            //}
            //else
            //{
            //    litLOMessage.Text = "<h1>Login Failed.</h1>Please check your login name and try logging in again.";
            //}
        }
    }
}